package com.Ajava1704.for线程池2;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 作者: LCG
 * 日期: 2020/2/11 09:41
 * 描述: 带定时的线程池
 */
public class ForPoolTest4 {
    public static void main(String[] args) {

        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("=======任务一========");
            }
        };

        scheduledExecutorService.scheduleWithFixedDelay(runnable,3,5,TimeUnit.SECONDS);
        /*第一次调度开始时间点=当前时间+initialDelay
        下一次调度开始时间点=上一次task完成时间点+delay*/


        scheduledExecutorService.scheduleAtFixedRate(runnable,3,5,TimeUnit.SECONDS);
        /*解释
        第一次调度开始时间点=当前时间+initialDelay
        第二次调度开始时间点=initialDelay + perid
        第n次调度开始时间点=initialDelay + n *perid*/

        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                System.out.println("=======任务二========");
            }
        };

        scheduledExecutorService.scheduleWithFixedDelay(runnable2,3,1,TimeUnit.SECONDS);

    }
}
